package com.dbn.webmagic.controller;

import com.dbn.webmagic.entity.Authorize;
import com.dbn.webmagic.service.AuthorizeService;
import com.dbn.webmagic.utils.ResultData;
import com.dbn.webmagic.pageprocessor.VarietyPipeline;
import com.dbn.webmagic.pageprocessor.VarietyProcessor;
import com.dbn.webmagic.utils.UrlDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.model.HttpRequestBody;
import us.codecraft.webmagic.utils.HttpConstant;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author liheping
 * @Description //爬取审定网站数据
 * @Date 2021/11/25 16:47
 **/
@RestController
@RequestMapping("/authorize")
@CrossOrigin
public class AuthorizeController {
    @Autowired
    private AuthorizeService authorizeService;
    @Resource
    private VarietyProcessor varietyProcessor;
    @Resource
    private VarietyPipeline varietyPipeline;


    //爬取品种审定网站数据
    @RequestMapping("/variety")
    public ResultData Variety(@RequestParam(value = "JudgementNo", required = false) String JudgementNo//审定编号
            , @RequestParam(value = "VarietyName", required = false) String VarietyName//品种名称
            , @RequestParam(value = "ApplyCompany", required = false) String ApplyCompany//申请单位
            , @RequestParam(value = "CropID", required = false) String CropID//作物名称
            , @RequestParam(value = "IsTransgenosis", required = false) String IsTransgenosis//转基因
            , @RequestParam(value = "JudgementRegionID", required = false) String JudgementRegionID//审定单位
            , @RequestParam(value = "introductionFilingType", required = false) String introductionFilingType) {//引种备案
        if (JudgementNo == null)
            JudgementNo = "";
        if (VarietyName == null)
            VarietyName = "";
        if (ApplyCompany == null)
            ApplyCompany = "";
        if (CropID == null)
            CropID = "";
        if (IsTransgenosis == null)
            IsTransgenosis = "";
        if (JudgementRegionID == null)
            JudgementRegionID = "";
        if (introductionFilingType == null)
            introductionFilingType = "";
        if(JudgementNo==""&&VarietyName==""&&ApplyCompany==""&&CropID=="")
            return ResultData.error(1,"请输入审定编号或品种名称或申请单位或作物名称来进行查询");
        List<Authorize> authorizeList = authorizeService.selectbyparam(JudgementNo, VarietyName, ApplyCompany, CropID);
        if (authorizeList != null) {
            return ResultData.ok().put("authorizeList", authorizeList);
        }
        Map<String, Object> params = new HashMap();
        params.put("JudgementNo", JudgementNo);
        params.put("VarietyName", VarietyName);
        params.put("ApplyCompany", ApplyCompany);
        params.put("CropID", CropID);
        params.put("IsTransgenosis", IsTransgenosis);
        params.put("JudgementRegionID", JudgementRegionID);
        params.put("introductionFilingType", introductionFilingType);
        String url = UrlDetail.AUTHFIRST + JudgementNo + "&VarietyNameLike=" + VarietyName + "&JudgementYear=&ApplyCompanyLike=" + ApplyCompany + "&CropID=" + CropID + "&IsTransgenosis=" + IsTransgenosis + "&IsExit=&JudgementRegionID=" + JudgementRegionID + "&introductionFilingType=" + introductionFilingType + "&_search=false&nd=1636427922524&rows=20&page=1&sidx=JudgementYear+desc+%2CRegionID%2CJudgementNo&sord=desc";
        Request firstRequest = new Request(url);
        firstRequest.setMethod(HttpConstant.Method.GET);
        //用于网页之间传参
        firstRequest.setExtras(params);
        firstRequest.setRequestBody(HttpRequestBody.form(params, "utf-8"));
        Spider.create(varietyProcessor)
                .addRequest(firstRequest)
                .addPipeline(varietyPipeline)
                .thread(10)
                .run();
        List<Authorize> authList = authorizeService.selectbyparam(JudgementNo, VarietyName, ApplyCompany, CropID);
        if (authList == null) {
            return ResultData.error(1,"暂无数据");
        }
        return ResultData.ok().put("authorizeList", authList);
    }
}
